package com.youyi.yyscreencutlibrary.Core;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.google.gson.Gson;
import com.lkx.liandianqinew.R;
import com.youyi.yyscreencutlibrary.SDK.YYScreenCutSDK;
import com.youyi.yyscreencutlibrary.ShowMainActivity;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class YYScreenCutActivity extends AppCompatActivity {
    private static int REQUEST_MEDIA_PROJECTION = 0;
    private static final String TAG = "YYScreenCutActivity00";
    private boolean hasSetBackBitmap;
    private YYScreenCutSDK.CutType mCutType;
    private CutMarkView mMarkSizeView;
    private DisplayMetrics mMetrics;
    private LinearLayout mMyMarkBottom;
    private Rect mRect;
    private YYScreenShotHelper mScreenShotHelper;

    /* access modifiers changed from: protected */
    @Override // android.support.v7.app.AppCompatActivity, android.support.v4.app.SupportActivity, android.support.v4.app.FragmentActivity
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        if (YYScreenCutSDK.mIsFinish) {
            EventBus.getDefault().post(new ShowMainActivity(true));
            finish();
            return;
        }
        setContentView(R.layout.yy_activity_screen_short);
        this.mMyMarkBottom = (LinearLayout) findViewById(R.id.mymark_bottom);
        ImageView imageView = (ImageView) findViewById(R.id.mymark_cancel);
        ImageView imageView2 = (ImageView) findViewById(R.id.mymark_done);
        this.mMarkSizeView = (CutMarkView) findViewById(R.id.mark_view);
        if (isScreenOriatationPortrait(this)) {
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) this.mMyMarkBottom.getLayoutParams();
            layoutParams.bottomMargin = getNavigationBarHeight(this);
            this.mMyMarkBottom.setLayoutParams(layoutParams);
        }
        this.mMarkSizeView.setmOnClickListener(new CutMarkView.onClickListener() {
            /* class com.youyi.yyscreencutlibrary.Core.YYScreenCutActivity.AnonymousClass1 */

            @Override // com.youyi.yyscreencutlibrary.Core.CutMarkView.onClickListener
            public void onCancel() {
                YYScreenCutActivity.this.mMyMarkBottom.setVisibility(View.VISIBLE);
                YYScreenCutActivity.this.updateButtonPosition();
            }

            @Override // com.youyi.yyscreencutlibrary.Core.CutMarkView.onClickListener
            public void onTouch() {
                YYScreenCutActivity.this.mMyMarkBottom.setVisibility(View.GONE);
            }
        });
        imageView.setOnClickListener(new View.OnClickListener() {
            /* class com.youyi.yyscreencutlibrary.Core.YYScreenCutActivity.AnonymousClass2 */

            public void onClick(View view) {
                if (YYScreenCutSDK.mOnCutListener != null) {
                    YYScreenCutSDK.mOnCutListener.result(false, null);
                    YYScreenCutSDK.mOnCutListener = null;
                }
                if (YYScreenCutSDK.mOnRectListener != null) {
                    YYScreenCutSDK.mOnRectListener.result(false, null);
                    YYScreenCutSDK.mOnRectListener = null;
                }
                YYScreenCutActivity.this.finishMethod();
            }
        });
        imageView2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                YYScreenCutActivity.this.getRectMethod();
            }
        });
        this.mMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getRealMetrics(this.mMetrics);
        YYScreenCutSDK.PixWidth = this.mMetrics.widthPixels;
        YYScreenCutSDK.PixHeight = this.mMetrics.heightPixels;
        YYScreenCutSDK.StarBarHeight = getStatusBarHeight(this);
        intentMethod(getIntent());
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void updateButtonPosition() {
        Rect rect = this.mMarkSizeView.getRect();
        int dip2px = dip2px(this, 50.0f);
        int dip2px2 = dip2px(this, 110.0f);
        getScreenWidth();
        int screenHeight = getScreenHeight();
        int statusBarHeight = getStatusBarHeight(this);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(dip2px2, dip2px);
        int width = rect.width();
        int height = rect.height();
        if (dip2px2 < width && dip2px < height) {
            layoutParams.topMargin = rect.bottom - dip2px;
            layoutParams.leftMargin = rect.right - dip2px2;
        } else if (rect.bottom + dip2px <= screenHeight) {
            layoutParams.topMargin = rect.bottom;
            if (rect.right > dip2px2) {
                layoutParams.leftMargin = rect.right - dip2px2;
            } else {
                layoutParams.leftMargin = rect.left;
            }
        } else if (rect.top > statusBarHeight + dip2px) {
            layoutParams.topMargin = rect.top - dip2px;
            if (rect.right > dip2px2) {
                layoutParams.leftMargin = rect.right - dip2px2;
            } else {
                layoutParams.leftMargin = rect.left;
            }
        } else {
            layoutParams.topMargin = rect.top + (height / 2);
            if (rect.right > dip2px2) {
                layoutParams.leftMargin = rect.right - dip2px2;
            } else {
                layoutParams.leftMargin = rect.left;
            }
        }
        this.mMyMarkBottom.setLayoutParams(layoutParams);
    }

    public static int getScreenWidth() {
        return Resources.getSystem().getDisplayMetrics().widthPixels;
    }

    public static int getScreenHeight() {
        return Resources.getSystem().getDisplayMetrics().heightPixels;
    }

    public int dip2px(Context context, float f) {
        return (int) ((f * context.getResources().getDisplayMetrics().density) + 0.5f);
    }

    public int px2dip(Context context, float f) {
        return (int) ((f / context.getResources().getDisplayMetrics().density) + 0.5f);
    }

    /* access modifiers changed from: protected */
    @Override // android.support.v4.app.FragmentActivity
    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        intentMethod(intent);
    }

    private void intentMethod(Intent intent) {
        String stringExtra = intent.getStringExtra("cutType");
        if (TextUtils.isEmpty(stringExtra)) {
            stringExtra = YYScreenCutSDK.CutType.CutFull.toString();
        }
        this.mCutType = YYScreenCutSDK.CutType.valueOf(stringExtra);
        String stringExtra2 = intent.getStringExtra("rectJson");
        if (!TextUtils.isEmpty(stringExtra2)) {
            this.mRect = (Rect) new Gson().fromJson(stringExtra2, Rect.class);
        }
        if (this.mCutType == YYScreenCutSDK.CutType.CutByData || this.mCutType == YYScreenCutSDK.CutType.CutFull) {
            this.mMarkSizeView.setVisibility(View.GONE);
            this.mMyMarkBottom.setVisibility(View.GONE);
            cut00();
            return;
        }
        this.mMarkSizeView.setVisibility(View.VISIBLE);
        this.mMyMarkBottom.setVisibility(View.VISIBLE);
        Rect rect = this.mRect;
        if (rect != null) {
            this.mMarkSizeView.setRect(rect);
        }
        updateButtonPosition();
    }

    private void getRectMethod() {
        Rect rect = this.mMarkSizeView.getRect();
        this.mMarkSizeView.setVisibility(View.GONE);
        this.mMyMarkBottom.setVisibility(View.GONE);
        this.mMarkSizeView.setUnmarkedColor(0);
        this.mMarkSizeView.setEnabled(false);
        int i = this.mCutType.ordinal();
        if (i == 1) {
            cut00();
        } else if (i == 2) {
            if (YYScreenCutSDK.mOnRectListener != null) {
                YYScreenCutSDK.mOnRectListener.result(true, rect);
                YYScreenCutSDK.mOnRectListener = null;
                finishMethod();
                return;
            }
            finishMethod();
        }
    }

    private void cut00() {

        if (!YYScreenCutSDK.hasPermission) {
            Log.d("lkx_debug","cut00:1");
            startCapture();
        } else if (Build.VERSION.SDK_INT >= 29) {
            Log.d("lkx_debug","cut00:2");
            YYScreenCutSDK.hasPermission = true;
            YYScreenCutSDK.mNowCutType = this.mCutType;
            YYScreenCutSDK.mNowMarkRect = this.mMarkSizeView.getRect();
            YYScreenCutSDK.mNowRect = this.mRect;
            startForegroundService(new Intent(this, CaptureScreenService.class));
        } else {
            Log.d("lkx_debug","cut00:3");
            cutMethod(YYScreenCutSDK.resultCode, YYScreenCutSDK.data);
        }
    }

    private void startCapture() {
        startActivityForResult(((MediaProjectionManager) getSystemService("media_projection")).createScreenCaptureIntent(), REQUEST_MEDIA_PROJECTION);
    }

    /* access modifiers changed from: protected */
    @Override // android.support.v4.app.FragmentActivity
    public void onActivityResult(int i, final int i2, final Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (i != REQUEST_MEDIA_PROJECTION) {
            return;
        }
        if (i2 != -1 || intent == null) {
            YYScreenCutSDK.hasPermission = false;
            if (YYScreenCutSDK.mOnCutListener != null) {
                YYScreenCutSDK.mOnCutListener.result(false, null);
                YYScreenCutSDK.mOnCutListener = null;
            }
            if (YYScreenCutSDK.mOnRectListener != null) {
                YYScreenCutSDK.mOnRectListener.result(false, null);
                YYScreenCutSDK.mOnRectListener = null;
            }
            finishMethod();
        } else if (Build.VERSION.SDK_INT >= 29) {
            new Handler().postDelayed(new Runnable() {
                /* class com.youyi.yyscreencutlibrary.Core.YYScreenCutActivity.AnonymousClass4 */

                public void run() {
                    YYScreenCutSDK.hasPermission = true;
                    YYScreenCutSDK.resultCode = i2;
                    YYScreenCutSDK.data = intent;
                    YYScreenCutSDK.mNowCutType = YYScreenCutActivity.this.mCutType;
                    YYScreenCutSDK.mNowMarkRect = YYScreenCutActivity.this.mMarkSizeView.getRect();
                    YYScreenCutSDK.mNowRect = YYScreenCutActivity.this.mRect;
                    YYScreenCutActivity.this.startForegroundService(new Intent(YYScreenCutActivity.this, CaptureScreenService.class));
                }
            }, 500);
        } else {
            this.mMetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getRealMetrics(this.mMetrics);
            YYScreenCutSDK.hasPermission = true;
            YYScreenCutSDK.resultCode = i2;
            YYScreenCutSDK.data = intent;
            new Handler().postDelayed(new Runnable() {
                /* class com.youyi.yyscreencutlibrary.Core.YYScreenCutActivity.AnonymousClass5 */

                public void run() {
                    YYScreenCutActivity.this.cutMethod(i2, intent);
                }
            }, 500);
        }
    }

    private void cutMethod(int i, Intent intent) {
        this.hasSetBackBitmap = false;
        YYScreenShotHelper yYScreenShotHelper = new YYScreenShotHelper(getApplicationContext(), i, intent, new YYScreenShotHelper.OnScreenShotListener() {

            @Override
            public void onFinish(final Bitmap bitmap) {
                YYScreenCutActivity.this.runOnUiThread(new Runnable() {
                    public void run() {
                        if (!YYScreenCutActivity.this.hasSetBackBitmap) {
                            YYScreenCutActivity.this.hasSetBackBitmap = true;
                            YYScreenCutActivity.this.resloveBitMap(bitmap);
                        }
                    }
                });
            }
        });
        this.mScreenShotHelper = yYScreenShotHelper;
        yYScreenShotHelper.startScreenShot();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(CutBitmap cutBitmap) {
        resloveBitMap(cutBitmap.getBitmap());
    }

    private void resloveBitMap(Bitmap bitmap) {
        try {
            Bitmap zoomImg = zoomImg(bitmap, YYScreenCutSDK.PixWidth);
            int i = this.mCutType.ordinal();
            if (i == 1) {
                Rect rect = this.mMarkSizeView.getRect();
                if (rect != null) {
                    int width = rect.left + rect.width();
                    int height = rect.top + rect.height();
                    if (width > zoomImg.getWidth() || height > zoomImg.getHeight()) {
                        Toast.makeText(this, "截取区域超出屏幕范围", 0).show();
                        if (YYScreenCutSDK.mOnCutListener != null) {
                            YYScreenCutSDK.mOnCutListener.result(false, null);
                            YYScreenCutSDK.mOnCutListener = null;
                        }
                    } else {
                        Bitmap createBitmap = Bitmap.createBitmap(zoomImg, rect.left, rect.top, rect.width(), rect.height());
                        if (YYScreenCutSDK.mOnCutListener != null) {
                            YYScreenCutSDK.mOnCutListener.result(true, createBitmap);
                            YYScreenCutSDK.mOnCutListener = null;
                        }
                    }
                } else if (YYScreenCutSDK.mOnCutListener != null) {
                    YYScreenCutSDK.mOnCutListener.result(false, null);
                    YYScreenCutSDK.mOnCutListener = null;
                }
            } else if (i != 3) {
                if (i == 4) {
                    int width2 = this.mRect.left + this.mRect.width();
                    int height2 = this.mRect.top + this.mRect.height();
                    if (width2 > zoomImg.getWidth() || height2 > zoomImg.getHeight()) {
                        Toast.makeText(this, "截取区域超出屏幕范围", 0).show();
                    } else {
                        Bitmap createBitmap2 = Bitmap.createBitmap(zoomImg, this.mRect.left, this.mRect.top, this.mRect.width(), this.mRect.height());
                        if (YYScreenCutSDK.mOnCutListener != null) {
                            YYScreenCutSDK.mOnCutListener.result(true, createBitmap2);
                            YYScreenCutSDK.mOnCutListener = null;
                        }
                    }
                }
            } else if (YYScreenCutSDK.mOnCutListener != null) {
                YYScreenCutSDK.mOnCutListener.result(true, zoomImg);
                YYScreenCutSDK.mOnCutListener = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            YYScreenCutSDK.hasPermission = false;
            finishMethod();
        } catch (Throwable th) {
            finishMethod();
            throw th;
        }
        finishMethod();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void finishMethod() {
        try {
            YYScreenCutSDK.mIsFinish = true;
            finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Bitmap zoomImg(Bitmap bitmap, int i) {
        float width = (float) bitmap.getWidth();
        Matrix matrix = new Matrix();
        float f = ((float) i) / width;
        matrix.postScale(f, f);
        return Bitmap.createBitmap(bitmap, 0, 0, (int) width, (int) ((float) bitmap.getHeight()), matrix, true);
    }

    public boolean isScreenOriatationPortrait(Context context) {
        return context.getResources().getConfiguration().orientation == 1;
    }

    public int getStatusBarHeight(Context context) {
        int identifier = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (identifier > 0) {
            return context.getResources().getDimensionPixelSize(identifier);
        }
        return (int) TypedValue.applyDimension(1, (float) 24, Resources.getSystem().getDisplayMetrics());
    }

    private int getNavigationBarHeight(Context context) {
        Resources resources = context.getResources();
        return resources.getDimensionPixelSize(resources.getIdentifier("navigation_bar_height", "dimen", "android"));
    }

    @Override // android.support.v7.app.AppCompatActivity
    public boolean onKeyDown(int i, KeyEvent keyEvent) {
        if (i != 4) {
            return super.onKeyDown(i, keyEvent);
        }
        if (YYScreenCutSDK.mOnCutListener != null) {
            YYScreenCutSDK.mOnCutListener.result(false, null);
            YYScreenCutSDK.mOnCutListener = null;
        }
        if (YYScreenCutSDK.mOnRectListener != null) {
            YYScreenCutSDK.mOnRectListener.result(false, null);
            YYScreenCutSDK.mOnRectListener = null;
        }
        finishMethod();
        return true;
    }

    /* access modifiers changed from: protected */
    @Override // android.support.v4.app.FragmentActivity
    public void onResume() {
        super.onResume();
    }

    /* access modifiers changed from: protected */
    public void onRestart() {
        super.onRestart();
    }

    /* access modifiers changed from: protected */
    @Override // android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        stopService(new Intent(this, CaptureScreenService.class));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(StartScreenCutBean startScreenCutBean) {
        cutMethod(YYScreenCutSDK.resultCode, YYScreenCutSDK.data);
    }
}
